package com.example.wanjialei.service.serviceimpl;

import com.example.wanjialei.mapper.SummaryMapper;
import com.example.wanjialei.pojo.Stock;
import com.example.wanjialei.pojo.Summary;
import com.example.wanjialei.service.SummaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class SummaryImpl implements SummaryService {
    @Resource
    private SummaryMapper summaryMapper;
//    联表查询
    @Override
    public Map<String, Object> getAllSummary(Summary summary) {
        Map<String,Object> map = new HashMap<>();
        // 进行分页设置：给分页框架的方法中传入当前页面数据和每页显示的件数
        PageHelper.startPage(summary.getPage(), summary.getRows());
        List<Summary> list = summaryMapper.selectAll(summary);
        // 将查询的结果进行分页
        PageInfo<Summary> pageInfo = new PageInfo <>(list);
        if (list.size() >= 1){
            map.put("flag",true);
            map.put("message","数据查询成功");
            // 获取分页后的数据
            map.put("rows", pageInfo.getList());
            // 获取总件数
            map.put("total", pageInfo.getTotal());
        }else{
            map.put("flag",false);
            map.put("message","数据查询失败");
            map.put("rows", pageInfo.getList());
            map.put("total", pageInfo.getTotal());
        }
        return map;
    }

    @Override
    public Map<String, Object> insert(Summary summary) {
        Map<String,Object> map  = new HashMap<>();
        int count = summaryMapper.insert2(summary);
        if(count>0){
            map.put("flag",true);
            map.put("message","插入成功！");
            map.put("total",count);
        }else{
            map.put("flag",false);
            map.put("message","插入失败！");
            map.put("total",count);
        }
        return map;
    }
}
